package com.fary.web.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue, String> {

    private final Set<String> set = new HashSet<>();

    @Override
    public void initialize(ListValue constraintAnnotation) {
        set.addAll(Arrays.asList(constraintAnnotation.value()));
    }

    /**
     * 判断是否通过校验
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
